﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.IO;
using log4net.Appender;
using System.Diagnostics;
using log4net.Core;

namespace Quebec.Logging.Services.Server.Appenders
{
	/// <summary>
	/// The standard log4net SmtpAppender doesn't support SSL authentication, which is
	/// required to send email via gmail.
	///
	/// This class is for internal use only.
	/// 
	/// This appender uses the SmtpClient (only available in .NET 2.0) to send SMTP mail that
	/// is secured via SSL.  This is needed to talk to the gmail SMTP server. 
	///
	/// This code is heavily based on that posted by Ron Grabowski at:
	/// http://mail-archives.apache.org/mod_mbox/logging-log4net-user/200602.mbox/%3C20060216123155.22007.qmail@web32202.mail.mud.yahoo.com%3E
	/// </summary>
	public class SmtpClientSmtpAppender : SmtpAppender
	{
		override protected void SendBuffer(LoggingEvent[] events)
		{
			try
			{
				StringWriter writer = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);

				string t = Layout.Header;

				if (t != null)
				{
					writer.Write(t);
				}

				for (int i = 0; i < events.Length; i++)
				{
					// Render the event and append the text to the buffer
					RenderLoggingEvent(writer, events[i]);
				}

				t = Layout.Footer;

				if (t != null)
				{
					writer.Write(t);
				}

				// Use SmtpClient so we can use SSL.
				SmtpClient client = new SmtpClient(SmtpHost, Port);
				client.EnableSsl = true;
				client.Credentials = new NetworkCredential(Username, Password);
				string messageText = writer.ToString();
				MailMessage mail = new MailMessage(From, To, Subject, messageText);
				client.SendAsync(mail, null);
			}

			catch (Exception e)
			{
				string msg = "Error occurred while sending e-mail notification from SmtpClientSmtpAppender.";
				Debug.WriteLine(msg);
				ErrorHandler.Error(msg, e);
			}
		}
	}
}
